<?php


namespace App\Admin\Actions\Post;


use App\Admin\Actions\RowAction;
use App\BookApply;
use App\Patent;
use App\PatentApply;
use App\PlantVariety;
use App\Transaction;
use Illuminate\Http\Request;

class TransactionPost extends RowAction
{
    public $name = '已转化';

    public function handle($model, Request $request)
    {
        $data = $request->only(['buyer', 'amount', 'type', 'date', 'contact_person', 'contact_mobile']);
        if ($model instanceof PlantVariety
            || $model instanceof PatentApply
            || $model instanceof Patent
            || $model instanceof BookApply
            || method_exists($model, 'addSale')
        ) {
            $model->addSale($data);
        } else {
            return $this->response()->swal()->error('系统错误')->refresh();
        }
        return $this->response()->swal()->success('操作成功')->refresh();
    }

    public function form()
    {
        $this->text('buyer', __('受让方'))->required();
        $this->text('amount', __('转化金额（万）'))->required();
        $this->select('type', __('转化类型'))->options(collect(PatentApply::PTM)->map(function ($vo) {
            return ['name' => $vo];
        })->pluck('name', 'name'));
        $this->date('date', __('转化日期'));
        $this->text('contact_person', __('联系人'))->default($this->row->user->name ?? '');
        $this->text('contact_mobile', __('联系电话'))->default($this->row->user->mobile ?? '');
    }

}
